#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

const char* menu[] =
{
	"a - add new record",
	"d - delete record",
	"q - quit",
	NULL
};

int getchoice(char* greet, const char* choices[]);

int main(void)
{
	int choice = 0;

	if (!isatty(fileno(stdout)))
	{
		fprintf(stderr, "You are not a termnial!\n");
		return 1;
	}

	do
	{
		choice = getchoice("Please select an action", menu);
		printf ("You have chose: %c\n", choice);
	}while (choice != 'q');

	return 0;
}

int getchoice(char* greet, const char* choices[])
{
	int chosen = 0;
	int selected;
	const char** option;

	do
	{
		printf ("Choice: %s\n", greet);
		option = choices;
		while (NULL != *option)
		{
			printf ("%s\n", *option);
			option++;
		}

		selected = getchar();
		option = choices;
		while (NULL != *option)
		{
			if (selected == *option[0])
			{
				chosen = 1;
				break;
			}
			option++;
		}
		if (0 == chosen)
		{
			printf ("Incorrect choice, select again\n");
		}
	}while (0 == chosen);

	return selected;
}
